/**
 */
package com.dingpeikeji.quickdeer.core.modules.realsche.web;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.utils.DateUtils;
import com.dingpeikeji.quickdeer.core.common.utils.StringUtils;
import com.dingpeikeji.quickdeer.core.common.web.BaseController;
import com.dingpeikeji.quickdeer.core.modules.bus.service.TCompBusService;
import com.dingpeikeji.quickdeer.core.modules.line.entity.TLine;
import com.dingpeikeji.quickdeer.core.modules.line.service.TLineService;
import com.dingpeikeji.quickdeer.core.modules.realsche.entity.TRealsche;
import com.dingpeikeji.quickdeer.core.modules.realsche.service.TRealscheService;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.site.service.TSitedtlService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.service.OfficeService;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.core.modules.viewspot.service.SysViewspotService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 实际班次调度Controller
 * @author lh
 * @version 2016-08-04
 */
@Controller
@RequestMapping(value = "${adminPath}/realsche/tRealscheModify")
public class TRealscheModifyController extends BaseController {

	@Autowired
	private TRealscheService tRealscheService;
	@Autowired
	private TSitedtlService tSitedtlservice; 
	@Autowired
	private TLineService tLineService;
	@Autowired
	private OfficeService officeService;
	@Autowired
	private TCompBusService compBusService;
	@Autowired
	private SysViewspotService viewsService;
	@Autowired
	private SysViewspotService sysViewspotService;
	@ModelAttribute
	public TRealsche get(@RequestParam(required=false) String id) {
		TRealsche entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tRealscheService.get(id);
		}
		if (entity == null){
			entity = new TRealsche();
		}
		return entity;
	}
	
	@RequiresPermissions("realsche:tRealscheModify:view")
	@RequestMapping(value = {"list", ""})
	public String list(TRealsche tRealsche, HttpServletRequest request, HttpServletResponse response, Model model) {
		if (tRealsche.getOfficeid() == null)
			tRealsche.setOfficeid(UserUtils.getUser().getOffice().getId());
		
		if (tRealsche.getSchekind() == null)
			tRealsche.setSchekind("1");
		
		Page<TRealsche> page = tRealscheService.findPage(new Page<TRealsche>(request, response), tRealsche); 
		model.addAttribute("page", page);
		return "modules/realsche/tRealscheModifyList";
	}

	@RequiresPermissions("realsche:tRealscheModify:view")
	@RequestMapping(value = "form")
	public String form(TRealsche tRealsche, String updateorview, Model model) {
		int i = 0;
		
		Date tomorrow = new Date();
		Calendar ca = new GregorianCalendar();
		ca.setTime(tomorrow);
		ca.add(ca.DATE, 1);
		tomorrow = ca.getTime();
		//初值
		if (tRealsche.getDepartdate() == null)
			tRealsche.setDepartdate(tomorrow);
		
		model.addAttribute("tomorrow", DateUtils.formatDate(tomorrow, "yyyy-MM-dd"));
		model.addAttribute("tRealsche", tRealsche);
		// 将上客点 车辆 信息通过json传递给页面
		// 将上客点 车辆 信息通过json传递给页面
		model.addAttribute("board", tRealsche.gettRealscheBoardList());
		model.addAttribute("boardnum", tRealsche.gettRealscheBoardList().size());
		model.addAttribute("off", tRealsche.gettRealscheOffList());
		model.addAttribute("offnum", tRealsche.gettRealscheOffList().size());
		model.addAttribute("veh", tRealsche.gettRealscheVehiList().size());
		
		// 查询站点
		model.addAttribute("tSitedList", tSitedtlservice.findListAll());
		// 查出所有客运公司
		List<Map<String, Object>> mapList = Lists.newArrayList();
		Office qryOffice = new Office();
		qryOffice.setFlatType(Office.FLAT_TYPE_BUS);
		List<Office> list = officeService.findListByTerm(qryOffice);
		for (i = 0; i < list.size(); i++) {
			Office e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", e.getId());
			map.put("pId", e.getParentId());
			map.put("pIds", e.getParentIds());
			map.put("name", e.getName());
			mapList.add(map);
		}
		model.addAttribute("officelist", mapList);

		model.addAttribute("updateorview", updateorview);
		model.addAttribute("today", DateUtils.getDate("yyyy-MM-dd")); // 当天日期字符串
		
		return "modules/realsche/tRealscheModifyForm";
	}
	
	
	@RequiresPermissions("realsche:tRealsche:edit")
	@RequestMapping(value = "save")
	public String save(TRealsche tRealsche, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, tRealsche)){
			return form(tRealsche, "", model);
		}
		//为城市赋值
/*				String departcitys = request.getParameter("departcitys");
				String destcitys = request.getParameter("destcitys");
				//当departcitycode不为空时 即是通过线路选择定义的 城市的
				if(!request.getParameter("departcitycode").equals("")){
					tRealsche.setDepartcity(request.getParameter("departcitycode"));
					tRealsche.setDestcity(request.getParameter("destcitycode"));
				}else {//当departcitycode为空时 即是通过自定义选择的城市
					tRealsche.setDepartcity(departcitys);
					tRealsche.setDestcity(destcitys);
				}*/
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmm");
		Date date = new Date();
		tRealsche.setSchecode(formatter.format(date));
		tRealsche.setCreateby(UserUtils.getUser().getId());
		tRealsche.setOfficeid(UserUtils.getUser().getOffice().getId());
		tRealsche.setCompanyid(UserUtils.getUser().getCompany().getId());
		tRealscheService.save(tRealsche);
		addMessage(redirectAttributes, "保存实际班次成功");
		return "redirect:"+Global.getAdminPath()+"/realsche/tRealscheModify/?repage";
	}
	
	@RequiresPermissions("realsche:tRealsche:edit")
	@RequestMapping(value = "delete")
	public String delete(TRealsche tRealsche, RedirectAttributes redirectAttributes) {
		tRealscheService.delete(tRealsche);
		addMessage(redirectAttributes, "删除实际班次成功");
		return "redirect:"+Global.getAdminPath()+"/realsche/tRealscheModify/?repage";
	}

}
